﻿using Autofac;
using Repository.Base;
using System;
using System.Linq;
using System.Reflection;
using Util;

namespace SceneEvidenceAPI.Models
{
    public class AutofacModuleRegister : Autofac.Module
    {
        /// <summary>
        /// 重写Autofac管道Load方法，在这里注册注入
        /// </summary>
        /// <param name="builder"></param>
        protected override void Load(ContainerBuilder builder)
        {
            //注册Service中的对象,Service中的类要以Service结尾，否则注册失败
            builder.RegisterAssemblyTypes(GetAssemblyByName("Service")).Where(a => a.Name.EndsWith("Service")).AsSelf();
            //注册Repository中的对象,Repository中的类要以Repository结尾，否则注册失败
            builder.RegisterAssemblyTypes(GetAssemblyByName("Repository")).Where(a => a.Name.EndsWith("Repository")).AsSelf();
            //单独注册
            builder.RegisterInstance(new Factory().Create(2, 3));
            builder.RegisterInstance(new SnowFlakeGenerator(3));
            builder.RegisterInstance(new DbContext());
        }
        /// <summary>
        /// 根据程序集名称获取程序集
        /// </summary>
        /// <param name="AssemblyName">程序集名称</param>
        /// <returns></returns>
        public static Assembly GetAssemblyByName(String AssemblyName)
        {
            return Assembly.Load(AssemblyName);
        }
    }
}
